package com.zhekou.web.ext;

import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.ExecutionContext;
import net.sourceforge.stripes.controller.Interceptor;
import net.sourceforge.stripes.controller.Intercepts;
import net.sourceforge.stripes.controller.LifecycleStage;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;


@Intercepts(LifecycleStage.BindingAndValidation)
public class PartialInterceptor implements Interceptor {
    public Resolution intercept(ExecutionContext context) throws Exception {
        Method method = context.getHandler();
        //guard condition
        if (method == null) {
            return context.proceed();
        }


        if (method.getAnnotation(Partial.class) != null || method.getDeclaringClass().getAnnotation(Partial.class) != null) {
            HttpServletRequest req = context.getActionBeanContext().getRequest();
            req.setAttribute("decorator", "none");
        }
        /*else {
            String uri = context.getActionBeanContext().getRequest().getRequestURI();
            if (uri.indexOf("/admin") >= 0) {
                HttpServletRequest req = context.getActionBeanContext().getRequest();
                req.setAttribute("decorator", "admin");
            }
        }*/
        return context.proceed();
    }
}
